package com.example.myapplication.toast;

import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication.R;

public class ToastActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.normal:
                Toast.makeText(this, "这是一个普通的toast", Toast.LENGTH_LONG).show();
                break;
            case R.id.positionSet:
                Toast toast = Toast.makeText(this, "这是一个自定义位置的toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
            case R.id.withImg:
                toast = Toast.makeText(this, "这是一个自定义位置的toast", Toast.LENGTH_LONG);
                LinearLayout toastLayout = (LinearLayout) toast.getView();

                ImageView child = new ImageView(this);
                child.setImageResource(R.mipmap.ic_launcher);
                toastLayout.addView(child);
                toast.show();
                break;

        }
    }
}